package com.bookstore.Controller;


import com.bookstore.Domain.Code;
import com.bookstore.Domain.Result;
import com.bookstore.Domain.User;
import com.bookstore.Service.UserService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.bookstore.Domain.Code.MSG_OK;

/**
 * @author 未来
 */
@CrossOrigin
@Controller
@RequestMapping("/users")
public class UserController {

    private final UserService userService;


    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 获取全部用户
     * @return ：用户集合
     */
    @GetMapping
    @ResponseBody
    public Result getAll() {
        List<User> users = userService.getUsers();
        Integer code = users != null ? Code.GET_OK : Code.GET_ERR;
        String msg = users != null ? Code.MSG_OK : Code.MSG_ERR;
        return new Result(code,users, msg);
    }

    /**
     * 根据账号登录
     * @param user_number ：账号
     * @return ：查询出来的数据库
     */
    @GetMapping("/{user_number}")
    @ResponseBody
    public Result getByUser_number(@PathVariable String user_number) {
        User user = userService.findByName(user_number);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? Code.MSG_OK : Code.MSG_ERR;
        return new Result(code,user, msg);
    }

    /**
     * 根据user_number注销账户
     * @param user_number
     * @return
     */
    @DeleteMapping("/{user_number}")
    @ResponseBody
    public Result delete(@PathVariable String user_number) {
        boolean flag = userService.deleteUser(user_number);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 删除包含的id值的数组一个一个删除
     * @param arr
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteUsers(@RequestBody int[] arr) {
        boolean flag = userService.deleteArr(arr);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 根据用户账号修改密码
     * @param user
     * @return ：数据集合
     */
    @PutMapping
    @ResponseBody
    public Result updateUser_password(@RequestBody User user) {
        boolean flag = userService.updateUser(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /**
     * 分页获取用户信息
     * @param page
     * @param number
     * @return
     */
    @GetMapping("/{page}/{number}")
    @ResponseBody
    public Result getPages(@ApiParam("Integer[页数]") @PathVariable Integer page, @ApiParam("Integer[每页数量]") @PathVariable Integer number) {
        List<User> users = userService.getPages(page, number);
        Integer code = users != null ? Code.GET_OK : Code.GET_ERR;
        String msg = users != null ? MSG_OK : Code.MSG_ERR;
        return new Result(code, users, msg);
    }

    /**
     * 存储新数据
     */
    @PostMapping
    @ResponseBody
    public Result save(@RequestBody User user) {
        boolean flag = userService.saveUser(user);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /**
     * 模糊查询用户信息
     * @param query:模糊查询
     * @return
     */
    @GetMapping("/search/{query}")
    @ResponseBody
    public Result getSearch( @PathVariable String query) {
        List<User> users = userService.searchUsers(query);
        Integer code = users != null ? Code.GET_OK : Code.GET_ERR;
        String msg = users != null ? MSG_OK : Code.MSG_ERR;
        return new Result(code, users, msg);
    }

}
